package com.yisu.auction.ewf.common.utils;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.yisu.auction.ewf.common.web.HttpWebClient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 高德地图导航工具类
 *
 * Created by nichole on 2017/3/25.
 */
public class NavigationUtil {

    /**
     * 距离测量
     *
     * @param url 距离测量服务URL
     * @param mapKey 用户在高德地图官网申请Web服务API类型KEY
     * @param origins 起点，规则： lon，lat（经度，纬度）， “,”分割，如117.500244, 40.417801，经纬度小数点不超过6位,支持100个坐标对，坐标对见用“| ”分隔
     * @param destination 终点，规则： lon，lat（经度，纬度）， “,”分割，如117.500244, 40.417801
     */
    public static DrivingDistanceResponse drivingDistance(String url, String mapKey, String origins, String destination) {
        Map<String, String> params = new HashMap<>();
        params.put("key", mapKey);
        params.put("origins", origins);
        params.put("destination", destination);
        try {
            String response = HttpWebClient.post(url, params);
            return new Gson().fromJson(response, DrivingDistanceResponse.class);
        } catch (Exception e) {
            return null;
        }
    }

    public class DrivingDistanceResponse{
        public static final int FAILURE = 0;
        public static final int SUCCESS = 1;

        private int status;
        private String info;
        @SerializedName("infocode")
        private String infoCode;
        private List<DistanceResult> results;

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public String getInfo() {
            return info;
        }

        public void setInfo(String info) {
            this.info = info;
        }

        public String getInfoCode() {
            return infoCode;
        }

        public void setInfoCode(String infoCode) {
            this.infoCode = infoCode;
        }

        public List<DistanceResult> getResults() {
            return results;
        }

        public void setResults(List<DistanceResult> results) {
            this.results = results;
        }

        @Override
        public String toString() {
            return "DrivingDistanceResponse{" +
                    "status=" + status +
                    ", info='" + info + '\'' +
                    ", infoCode='" + infoCode + '\'' +
                    ", results=" + results +
                    '}';
        }
    }

    public class DistanceResult {
        @SerializedName("origin_id")
        private int originIndex;
        @SerializedName("dest_id")
        private int destIndex;
        private int distance;

        public int getOriginIndex() {
            return originIndex;
        }

        public void setOriginIndex(int originIndex) {
            this.originIndex = originIndex;
        }

        public int getDestIndex() {
            return destIndex;
        }

        public void setDestIndex(int destIndex) {
            this.destIndex = destIndex;
        }

        public int getDistance() {
            return distance;
        }

        public void setDistance(int distance) {
            this.distance = distance;
        }

        @Override
        public String toString() {
            return "DistanceResult{" +
                    "originIndex=" + originIndex +
                    ", destIndex=" + destIndex +
                    ", distance=" + distance +
                    '}';
        }
    }


    public static void main(String[] args) {
        DrivingDistanceResponse response = drivingDistance("http://restapi.amap.com/v3/distance", "45f4a8b6474ea3939e19d677d3edf866", "120.120282,30.278608|120.167798,29.659752|", "120.15507,30.274084");
        System.out.println(response.toString());
    }
}
